import { login } from '../../api/user'
import { hexMD5 } from "../../utils/md5.js"
import { lockSysUser } from '../../api/member'
const app = getApp()

Page({
    data: {
        disabled: true,
        btnstate: 'default',
        account: '',
        password: '',
        showPassword: true,
        verifyCode:'',
        verifyCodeKey:''
    },
    login(){
        let username = 'mch_'+ app.globalData.mchId +'_'+ this.data.account.trim()
        let params={
            username: username,
            password: hexMD5(this.data.password),
            verifyCode: this.data.verifyCode,
            verifyCodeKey: this.data.verifyCodeKey
        }
        login(params).then(res => {
            console.log(res)
            if (res.type == 'success') {
                const {
                  userInfo,
                  token
                } = res.data
                app.globalData.token = token
                 app.globalData.userInfo = userInfo
                this.startLockSysUser()
                wx.switchTab({
                  url: '/pages/index/index'
                })
            }
        })
    },
    startLockSysUser() {
        lockSysUser({ mchId: app.ISV_ID, xuid: app.globalData.xuid ,shopId:app.globalData.shopId}).then(res => {
          console.log(res)
          wx.showToast({
            title: res.content,
            icon: 'none',
            duration: 2000
          })
        })
      },
    showPasswordClick(){
        this.data.showPassword = ! this.data.showPassword
        this.setData({
            showPassword: this.data.showPassword
        });
    },
    accountInput (event) {
        this.data.account = event.detail.value.trim();
        this.setData({
            account:this.data.account
        })
    },
    pwdBlur: function(e) {
        var password = e.detail.value;
        if (password != '') {
            this.setData({
                password: password
            });
        }
        if (password !== '') {
            this.setData({
                disabled: false,
                btnstate: 'primary'
            });
        } else {
            this.setData({
                disabled: true,
                btnstate: 'default'
            });
        }
    },
    LogAndReg(){
        wx.navigateTo({
            url: '/pages/login/msgLogin'
        })
    }
})
